﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Small.Html.Options;

namespace Small.Html.UI
{
    public static class HtmlAjaxUpload
    {

        /// <summary>
        /// Ajax文件上传
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="id"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static MvcHtmlString AjaxUpload(this HtmlHelper helper, string id, AjaxUploadOptions options)
        {

            return new MvcHtmlString(GenerateScripts(id, null, null, options));
        }
        /// <summary>
        /// Ajax文件上传
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="className"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static MvcHtmlString AjaxUploadByClassName(this HtmlHelper helper, string className, AjaxUploadOptions options)
        {

            return new MvcHtmlString(GenerateScripts(null, className, null, options));
        }

        /// <summary>
        /// Ajax文件上传
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="className"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static MvcHtmlString AjaxUploadByElement(this HtmlHelper helper, string className, AjaxUploadOptions options)
        {

            return new MvcHtmlString(GenerateScripts(null, className, null, options));
        }

      

        private static string GenerateScripts(string id, string classname, string otherelement, AjaxUploadOptions options)
        {
            StringBuilder builder = new StringBuilder();

            if (!string.IsNullOrEmpty(id))
            {
                builder.Append("$('#" + id + "').uploadify({").AppendLine();
            }
            else if (!string.IsNullOrEmpty(classname))
            {
                builder.Append("$('." + classname + "').uploadify({").AppendLine();
            }
            else if (!string.IsNullOrEmpty(otherelement))
            {
                builder.Append("$('" + otherelement + "').uploadify({").AppendLine();
            }
            else
            {
                throw new Exception("没有适合的jQuery选择器");
            }

            builder.AppendLine("		swf:'" + options.Swf + "',");  // The path to the uploadify SWF file
            builder.AppendLine("		uploader:'" + options.Url + "',");  // The path to the server-side upload script


            if (options.Auto)
            {
                builder.AppendLine("		auto:false,");               // Automatically upload files when added to the queue
            }
            if (!String.IsNullOrEmpty(options.ButtonClass))
            {
                builder.AppendLine("		buttonClass:'" + options.ButtonClass + "',");                 // A class name to add to the browse button DOM object
            }

            if (!String.IsNullOrEmpty(options.ButtonCursor))
            {
                builder.AppendLine("		buttonCursor:'" + options.ButtonCursor + "',");             // The cursor to use with the browse button
            }

            if (!String.IsNullOrEmpty(options.ButtonImage))
            {
                builder.AppendLine("		buttonImage:" + options.ButtonImage + ",");
                // (String or null) The path to an image to use for the Flash browse button if not using CSS to style the button
            }


            builder.AppendLine("		buttonText:'" + (string.IsNullOrEmpty(options.ButtonText) ? "SELECT FILES" : options.ButtonText) + "',");     // The text to use for the browse button


            if (options.CheckExisting)
            {
                builder.AppendLine("		checkExisting:true,");              // The path to a server-side script that checks for existing files on the server
            }

            if (options.Debug)
            {
                builder.AppendLine("		debug:ture,");              // Turn on swfUpload debugging mode
            }

            if(!String.IsNullOrEmpty(options.FileObjName)){
                builder.AppendLine("		fileObjName:'" + options.FileObjName + "',");         // The name of the file object to use in your server-side script
            }
            if(options.FileSizeLimit>0){
                builder.AppendLine("		fileSizeLimit:" + options.FileSizeLimit + ",");                  // The maximum size of an uploadable file in KB (Accepts units B KB MB GB if string,0 for no limit)
            }

            if(!string.IsNullOrEmpty(options.FileTypeDesc)){
                builder.AppendLine("		fileTypeDesc:'" + options.FileTypeDesc + "',");
            }
            // The description for file types in the browse dialog

            if(!String.IsNullOrEmpty(options.FileTypeExts)){
                builder.AppendLine("		fileTypeExts:'" + options.FileTypeExts + "',");
            }
            // Allowed extensions in the browse dialog (server-side validation should also be used)
            builder.AppendLine("		height:" + (options.Height == 0 ? 30 : options.Height) + ",");
            // The height of the browse button  
            if (!String.IsNullOrEmpty(options.ItemTemplate))
            {
                builder.AppendLine("		itemTemplate:" + options.ItemTemplate + ",");
            }
            // The template for the file item in the queue

            if (!String.IsNullOrEmpty(options.Method))
            {
                builder.AppendLine("		method:'" + options.Method + "',");
            }
            // The method to use when sending files to the server-side upload script

            if (options.Multi)
            {

                builder.AppendLine("		multi:false,");
            }
            // Allow multiple file selection in the browse dialog 

            if (!String.IsNullOrEmpty(options.FormData))
            {
                builder.AppendLine("		formData:" + options.FormData + ",");
                // An object with additional data to send to the server-side upload script with every file upload
            }

            if (options.PreventCaching)
            {
                builder.AppendLine("		preventCaching:false,");
            }
            // Adds a random value to the Flash URL to prevent caching of it (conflicts with existing parameters)

            if (!String.IsNullOrEmpty(options.ProgressData))
            {
                builder.AppendLine("		progressData:'" + options.ProgressData + "',");
            }
            // ('percentage' or 'speed') Data to show in the queue item during a file upload

            if (!String.IsNullOrEmpty(options.QueueID))
            {
                builder.AppendLine("		queueID:" + options.QueueID + ",");
            }
            // The ID of the DOM object to use as a file queue (without the #)


            if(options.QueueSizeLimit > 0 ){
                builder.AppendLine("		queueSizeLimit:" + options.QueueSizeLimit + ",");
            // The maximum number of files that can be in the queue at one time
            }

            if (options.RemoveCompleted)
            {

                builder.AppendLine("		removeCompleted:false,");
            }

            if(options.RemoveTimeout > 0 ){
            // Remove queue items from the queue when they are done uploading
                builder.AppendLine("		removeTimeout:" + options.RemoveTimeout + ",");
            // The delay in seconds before removing a queue item if removeCompleted is set to true
            }
            if (options.RequeueErrors)
            {
                builder.AppendLine("		requeueErrors:true,");
            }
            // Keep errored files in the queue and keep trying to upload them

            if(options.SuccessTimeout > 0 ){
                builder.AppendLine("		successTimeout:" + options.SuccessTimeout + ",");
            }
            // The number of seconds to wait for Flash to 
            //detect the server's response after the file has finished uploading
            if(options.UploadLimit > 0){
                builder.AppendLine("		uploadLimit:" + options.UploadLimit + ",");
            }
            // The maximum number of files you can upload
            if(options.Width >0){
                builder.AppendLine("		width:" + options.Width + ",");
            // The width of the browse button
            }
            if (!String.IsNullOrEmpty(options.OnCancel))
            {
                builder.AppendLine("		onCancel:" + options.OnCancel + ",");
            }
            // Triggered when a file is cancelled from the queue
            if (!String.IsNullOrEmpty(options.OnClearQueue))
            {
                builder.AppendLine("		onClearQueue:" + options.OnClearQueue + "                     ");

            }
            if (!String.IsNullOrEmpty(options.OnDestroy))
            {
                builder.AppendLine("		onDestroy:" + options.OnDestroy + ",");

            }
            // Triggered when the uploadify object is destroyed

            if (!String.IsNullOrEmpty(options.OnDialogClose))
            {
                builder.AppendLine("		onDialogClose:" + options.OnDialogClose + ",");
                // Triggered when the browse dialog is closed
            }
            if (!String.IsNullOrEmpty(options.OnDialogOpen))
            {
                builder.AppendLine("		onDialogOpen:" + options.OnDialogOpen + ",");
            }
            // Triggered when the browse dialog is opened

            if (!String.IsNullOrEmpty(options.OnDisable))
            {
                builder.AppendLine("		onDisable:" + options.OnDisable + ",");
            }
            // Triggered when the browse button gets disabled

            if (!String.IsNullOrEmpty(options.OnEnable))
            {
                builder.AppendLine("		onEnable:" + options.OnEnable + "            ");
                // Triggered when the browse button gets enabled
            }
            if (!String.IsNullOrEmpty(options.OnFallback))
            {
                builder.AppendLine("		onFallback:" + options.OnFallback + ",");
            }
            // Triggered is Flash is not detected    
            if (!String.IsNullOrEmpty(options.OnInit))
            {
                builder.AppendLine("		onInit:" + options.OnInit + ",");
                // Triggered when Uploadify is initialized
            }
            if (!String.IsNullOrEmpty(options.OnQueueComplete))
            {
                builder.AppendLine("		onQueueComplete:" + options.OnQueueComplete + ",");
            }
            // Triggered when all files in the queue have been uploaded
            if (!String.IsNullOrEmpty(options.OnSelectError))
            {
                builder.AppendLine("		onSelectError:" + options.OnSelectError + ",");
            }
            // Triggered when an error occurs while selecting a file (file size,queue size limit,etc.)
            if (!String.IsNullOrEmpty(options.OnSelect))
            {
                builder.AppendLine("		onSelect:" + options.OnSelect + ",");
            }
            // Triggered for each file that is selected
            if (!String.IsNullOrEmpty(options.OnSWFReady))
            {
                builder.AppendLine("		onSWFReady:" + options.OnSWFReady + ",");
            }
            // Triggered when the SWF button is loaded
            if (!String.IsNullOrEmpty(options.OnUploadComplete))
            {
                builder.AppendLine("		onUploadComplete:" + options.OnUploadComplete + ",");
            }
            // Triggered when a file upload completes (success or error)
            if (!String.IsNullOrEmpty(options.OnUploadError))
            {
                builder.AppendLine("		onUploadError:" + options.OnUploadError + ",");
                // Triggered when a file upload returns an error
            } if (!String.IsNullOrEmpty(options.OnUploadSuccess))
            {
                builder.AppendLine("		onUploadSuccess:" + options.OnUploadSuccess + ",");
                // Triggered when a file is uploaded successfully
            } if (!String.IsNullOrEmpty(options.OnUploadProgress))
            {
                builder.AppendLine("		onUploadProgress:" + options.OnUploadProgress + ",");
                // Triggered every time a file progress is updated
            } if (!String.IsNullOrEmpty(options.OnUploadStart))
            {
                builder.AppendLine("		onUploadStart:" + options.OnUploadStart + ",");
            }
            // Triggered immediately before a file upload starts


             string result=builder.ToString().TrimEnd(',');

             result += "		});" + Environment.NewLine;


            return result;
        }
    }
}
